add gnu attribute to check functions with printf like parameters. (#1129)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Wed, 14 Jun 2023 11:32:03 +0000 (05:32 -0600)
committerGitHub <noreply@github.com>
Wed, 14 Jun 2023 11:32:03 +0000 (05:32 -0600)
and fix a bug revealed by the warning.

dg-100.h
gbser_private.h
gtrnctr.h
jeeps/gpsprot.cc
jeeps/gpsserial.cc
jeeps/gpsutil.h
mtk_logger.cc
skytraq.h
v900.cc
wbt-200.cc

index 7e3b5f438a4a3cccf1e4e5c6cd807234840fa068..12c609712edf33cac4882c991515382ac7a77279 100644 (file)
--- a/dg-100.h
+++ b/dg-100.h
@@ -110,7 +110,7 @@ protected:
   const dg100_command* dg100_findcmd(int id) const;
   static QDateTime bintime2utc(int date, int time);
   static void dg100_debug(const char* hdr, int include_nl, size_t sz, unsigned char* buf);
-  static void dg100_log(const char* fmt, ...);
+  [[gnu::format(printf, 1, 2)]] static void dg100_log(const char* fmt, ...);
   static float bin2deg(int val);
   void process_gpsfile(uint8_t* data, route_head** track) const;
   static uint16_t dg100_checksum(const uint8_t* buf, int count);
index e69de4db2526b500cbe3463f63255068b26a3fe3..acc10ae7041a1c40bcf594a9495ec7c67a187ff2 100644 (file)
@@ -24,7 +24,7 @@
 #define MYMAGIC 0x91827364
 #define BUFSIZE 512
 
-void gbser_db(int l, const char* msg, ...);
+[[gnu::format(printf, 2, 3)]] void gbser_db(int l, const char* msg, ...);
 int gbser_fill_buffer(void* handle, unsigned want, unsigned* ms);
 unsigned gbser_read_buffer(void* handle, void** buf, unsigned* len);
 #endif // GBSER_PRIVATE_H_
index 8556f1c500b56492a4323e07fb3c073b8203170b..28a11d1080d81055609d04fdc48b5d9286445b35 100644 (file)
--- a/gtrnctr.h
+++ b/gtrnctr.h
@@ -85,7 +85,7 @@ private:
 
   /* Member Functions */
 
-  void gtc_write_xml(int indent, const char* fmt, ...);
+  [[gnu::format(printf, 3, 4)]] void gtc_write_xml(int indent, const char* fmt, ...);
   void gtc_write_xml(int indent, const QString& s);
   void gtc_lap_start(const route_head*  /* unused */);
   static computed_trkdata gtc_new_study_lap(const route_head* rte);
index 91332bfee5fd8a6d8db385b5746508e15a23fdc2..d27dd26af850f2b7ece9155b2629ab938db4ece9 100644 (file)
@@ -348,7 +348,6 @@ int32 GPS_Protocol_Table_Set(US id)
 {
   int32 i;
   US  v;
-  char s[GPS_ARB_LEN];
 
   i=0;
   while ((v=GPS_MP[i].id)) {
@@ -372,8 +371,7 @@ int32 GPS_Protocol_Table_Set(US id)
   }
 
 
-  (void) snprintf(s, sizeof(s), "INIT: No table entry for ID %d\n", id);
-  GPS_Error(s);
+  GPS_Error("INIT: No table entry for ID %d\n", id);
 
   return GPS_UNSUPPORTED;
 }
@@ -392,11 +390,7 @@ int32 GPS_Protocol_Table_Set(US id)
 
 void GPS_Protocol_Error(US tag, US data)
 {
-  char s[GPS_ARB_LEN];
-
-  (void) snprintf(s, sizeof(s),
-                 "PROTOCOL ERROR: Unknown tag/data [%c/%d]\n", tag, data);
-  GPS_Error(s);
+  GPS_Error("PROTOCOL ERROR: Unknown tag/data [%c/%d]\n", tag, data);
 
   if (gps_n_tag_unknown < GPS_TAGUNK) {
     gps_tag_unknown[gps_n_tag_unknown] = tag;
index 4bbd488d52f20127357e2a9eb855c53965dea7c7..d0f8107abcf3ab36541b7715641e6f8a9b14749d 100644 (file)
@@ -70,22 +70,23 @@ typedef struct {
 /*
  * Display an error from the serial subsystem.
  */
-void GPS_Serial_Error(const char* mb, ...)
+void GPS_Serial_Error(const char* fmt, ...)
 {
   va_list ap;
   char msg[200];
   char* s;
   int b;
 
-  va_start(ap, mb);
-  b = vsnprintf(msg, sizeof(msg), mb, ap);
+  va_start(ap, fmt);
+  b = vsnprintf(msg, sizeof(msg), fmt, ap);
   s = msg + b;
   *s++ = ':';
   *s++ = ' ';
 
-  FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0,
-                GetLastError(), 0, s, sizeof(msg) - b - 2, 0);
-  GPS_Error(msg);
+  FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr,
+                GetLastError(), 0, s, sizeof(msg) - b - 2, nullptr);
+
+  GPS_Error("%s", msg); // valid clang -Wformat-security warning
 
   va_end(ap);
 }
@@ -379,24 +380,17 @@ int32 GPS_Serial_Open(gpsdevh* dh, const char* port)
 /*
  * Display an error from the serial subsystem.
  */
-void GPS_Serial_Error(const char* mb, ...)
+void GPS_Serial_Error(const char* fmt, ...)
 {
   va_list ap;
   char msg[200];
-  char* s;
-  int b;
 
-  va_start(ap, mb);
-  b = vsnprintf(msg, sizeof(msg), mb, ap);
-  s = msg + b;
-  *s++ = ':';
-  *s++ = ' ';
-  *s++ = '\0';
+  va_start(ap, fmt);
+  vsnprintf(msg, sizeof(msg), fmt, ap);
 
 //     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0,
 //                     GetLastError(), 0, s, sizeof(msg) - b - 2, 0 );
-  strcat(msg, strerror(errno));
-  GPS_Error(msg);
+  GPS_Error("%s: %s", msg, strerror(errno));
   va_end(ap);
 }
 
index c51e5b03f0fe3b0d1340bd2ed4152295ac447f49..741d31a5900c0f2b80af205ee1002b422fa7cd61 100644 (file)
   uint32 GPS_Util_Get_Uint(const UC* s);
 
   void   GPS_Warning(const char* s);
-  void   GPS_Error(const char* fmt, ...);
-  void   GPS_Serial_Error(const char* hdr, ...);
+  [[gnu::format(printf, 1, 2)]] void   GPS_Error(const char* fmt, ...);
+  [[gnu::format(printf, 1, 2)]] void   GPS_Serial_Error(const char* fmt, ...);
   void   GPS_Fatal(const char* s);
   void   GPS_Enable_Error();
   void   GPS_Enable_Warning();
   void   GPS_Disable_Error();
   void   GPS_Disable_Warning();
-  void   GPS_User(const char* fmt, ...);
+  [[gnu::format(printf, 1, 2)]] void   GPS_User(const char* fmt, ...);
   void   GPS_Disable_User();
   void   GPS_Enable_User();
   void   GPS_Diagnose(int32 c);
-  void   GPS_Diag(const char* fmt, ...);
+  [[gnu::format(printf, 1, 2)]] void   GPS_Diag(const char* fmt, ...);
 
   void   GPS_Enable_Diagnose();
   void   GPS_Disable_Diagnose();
index c6b7c906f4c9aef1754d7afbc5d1f2a8ea45601f..d0a15a10257af4e6852fec659b3017fa70678394 100644 (file)
@@ -281,7 +281,7 @@ static QVector<arglist_t> mtk_sargs = {
   },
 };
 
-static void dbg(int l, const char* msg, ...)
+[[gnu::format(printf, 2, 3)]] static void dbg(int l, const char* msg, ...)
 {
   va_list ap;
   va_start(ap, msg);
index 89c7c4bd43fb303c4076305438efdb460ce8d4b3..7c400f32b8c7aebba850f34d3ad3ce9dea3169eb 100644 (file)
--- a/skytraq.h
+++ b/skytraq.h
@@ -110,7 +110,7 @@ protected:
 
   /* Member Functions */
 
-  static void db(int l, const char* msg, ...);
+  [[gnu::format(printf, 2, 3)]] static void db(int l, const char* msg, ...);
   void rd_drain() const;
   int rd_char(int* errors) const;
   int rd_buf(uint8_t* buf, int len) const;
diff --git a/v900.cc b/v900.cc
index bb16a4763f63245be82695ced8bca3b460bc5288..e11129041432e5a25d25c38ff4c82f1eeb44396c 100644 (file)
--- a/v900.cc
+++ b/v900.cc
@@ -140,7 +140,7 @@ struct one_line_basic_mode {
 static FILE* fin = nullptr;
 
 /* copied from dg-100.cpp */
-static void
+[[gnu::format(printf, 1, 2)]] static void
 v900_log(const char* fmt, ...)
 {
   va_list ap;
index 98b63dcb8a50b706baa561e3a587bf831f58a449..75f16aff124154ab6f0c63059fcf123536f3cf4c 100644 (file)
@@ -145,7 +145,7 @@ struct read_state {
   struct buf_head     data;
 };
 
-static void db(int l, const char* msg, ...)
+[[gnu::format(printf, 2, 3)]] static void db(int l, const char* msg, ...)
 {
   va_list ap;
   va_start(ap, msg);
@@ -893,7 +893,7 @@ static int wbt201_read_chunk(struct read_state* st, unsigned pos, unsigned limit
   }
 
   if (cs != st->data.checksum) {
-    db(2, "Checksums don't match. Got %02lx, expected %02\n", cs, st->data.checksum);
+    db(2, "Checksums don't match. Got %02lx, expected %02x\n", cs, st->data.checksum);
     return 0;
   }